package com.example.smart_city_0829.activity.waste_sorting;

import android.content.Context;
import android.content.Intent;
import android.icu.util.LocaleData;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.waste_sorting.News;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeLv;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class WasteSortingMainActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private Banner banner;
    private ImageView searchIm;
    private ImageView typeIm;
    private MaxSizeLv xss;
    private MaxSizeLv dyzxd;
    private MaxSizeLv fldr;
    private MaxSizeLv sqdt;

    private List<com.example.smart_city_0829.entity.waste_sorting.Banner.DataBean> bannerData;
    private List<News.RowsBean> rows;
    private DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_waste_sorting_main;
    }

    @Override
    protected void initView() {
        banner = (Banner) findViewById(R.id.banner);
        searchIm = (ImageView) findViewById(R.id.search_im);
        typeIm = (ImageView) findViewById(R.id.type_im);
        xss = (MaxSizeLv) findViewById(R.id.xss);
        dyzxd = (MaxSizeLv) findViewById(R.id.dyzxd);
        fldr = (MaxSizeLv) findViewById(R.id.fldr);
        sqdt = (MaxSizeLv) findViewById(R.id.sqdt);
    }

    @Override
    protected void initData() {
        api.getGarbageBannerData().enqueue(new NetCallBack<com.example.smart_city_0829.entity.waste_sorting.Banner>() {
            @Override
            public void onResponse(Call<com.example.smart_city_0829.entity.waste_sorting.Banner> call, Response<com.example.smart_city_0829.entity.waste_sorting.Banner> response) {
                if (response.body() == null) return;

                bannerData = response.body().getData();
                banner.setImages(bannerData.stream()
                        .map(com.example.smart_city_0829.entity.waste_sorting.Banner.DataBean::getImgUrl)
                        .collect(Collectors.toList()));
                banner.setBannerTitles(bannerData.stream()
                        .map(com.example.smart_city_0829.entity.waste_sorting.Banner.DataBean::getTitle)
                        .collect(Collectors.toList()));
                banner.start();
                banner.startAutoPlay();
            }
        });

        api.getGarbageNews().enqueue(new NetCallBack<News>() {
            @Override
            public void onResponse(Call<News> call, Response<News> response) {
                if (response.body() == null) return;
                rows = response.body().getRows();

                final Map<Integer, List<News.RowsBean>> collect = rows.stream().collect(Collectors.groupingBy(News.RowsBean::getType));
                collect.replaceAll((i, v) -> Objects.requireNonNull(collect.get(i)).stream()
                        .sorted(new Comparator<News.RowsBean>() {
                            @Override
                            public int compare(News.RowsBean o1, News.RowsBean o2) {
                                return o2.getCreateTime().compareTo(o1.getCreateTime());
                            }
                        })
                        .collect(Collectors.toList()));
                xss.setAdapter(new ListAdapter(collect.get(7), WasteSortingMainActivity.this));
                dyzxd.setAdapter(new ListAdapter(collect.get(8), WasteSortingMainActivity.this));
                fldr.setAdapter(new ListAdapter(collect.get(9), WasteSortingMainActivity.this));
                sqdt.setAdapter(new ListAdapter(collect.get(10), WasteSortingMainActivity.this));
                xss.setOnItemClickListener(WasteSortingMainActivity.this);
                dyzxd.setOnItemClickListener(WasteSortingMainActivity.this);
                fldr.setOnItemClickListener(WasteSortingMainActivity.this);
                sqdt.setOnItemClickListener(WasteSortingMainActivity.this);

            }
        });
    }

    @Override
    protected void initEvent() {
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context)
                        .load(Constant.BASE_URL + o)
                        .into(imageView);
            }
        });
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);

        searchIm.setOnClickListener((v) -> {
            migrateTo(SearchActivity.class);
        });
        typeIm.setOnClickListener((v) -> {
            migrateTo(TypeActivity.class);
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "垃圾分类宝典";
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final News.RowsBean item = (News.RowsBean) parent.getAdapter().getItem(position);
        final Intent intent = new Intent(this, NewsDetailsActivity.class);
        intent.putExtra("id", item.getId());
        startActivity(intent);
    }

    static final class ListAdapter extends CommonAdapter<News.RowsBean, ListViewHolder> {

        public ListAdapter(List<News.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(News.RowsBean data, int position, ListViewHolder viewHolder, View convertView, Context context) {
            viewHolder.title.setMaxLines(2);
            viewHolder.title.setEllipsize(TextUtils.TruncateAt.END);

            viewHolder.title.setText(data.getTitle());
            viewHolder.content.setText(data.getCreateTime());

            Glide.with(context)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
        }

        @NonNull
        @Override
        protected ListViewHolder getViewHolder() {
            return new ListViewHolder();
        }

        @Override
        protected void initView(ListViewHolder viewHolder, View convertView) {
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
        }

    }

    static class ListViewHolder {
        TextView title;
        ImageView im;
        TextView content;
        TextView tag1;
        TextView tag2;
        TextView tag3;
    }
}